package games.ninJumpDx.ui.actions
{
	import core.Global;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.utils.getTimer;
	
	import games.ninJumpDx.data.GameData;
	import games.ninJumpDx.data.UIConfig;
	import games.ninJumpDx.tools.Tools;
	import utils.ActionFactory;
	import games.ninJumpDx.ui.factorys.BuildingFactory;
	import games.ninJumpDx.ui.factorys.ExplodeParticlesFactory;
	import games.ninJumpDx.ui.factorys.GargoyleFactory;
	
	import utils.Cartoon;
	
	public class GargoyleFrozen extends Cartoon
	{
		private var m_info:XMLList;
		private var m_actionFactory:ActionFactory;
		private var m_limit:Number;
		private var m_unfreeze:Boolean;
		public function GargoyleFrozen(name:String)
		{
			super();
			//m_actionFactory = new BuildingFactory();
			//addChild(m_actionFactory.makeAction(name));
			m_unfreeze = false;
			addChild(Tools.getBitmapByName(name,GFrame.MAIN_UI));
			m_info = UIConfig.getInfoByName(name);
			x = m_info.@x;
			
			y = UIConfig.getInfoByName(name).@y;
			if(m_info.hasOwnProperty("@scaleX"))
			{
				scaleX = m_info.@scaleX;
			}
			m_limit = UIConfig.getGameHeight() * .5;
			
		}
		
		override public function update(t:Object=null):void
		{
			if(!start || pause)return;
			if(y > m_limit && !m_unfreeze)
			{
				//destroy();
				unfreeze();
				return ;
			}
			
			if(m_unfreeze && numChildren <= 0)
			{
				destroy();
			}
			
			y += GameData.instance.speed ;
				
			
		}
		
		override public function destroy(e:* = null):void
		{
			super.destroy();
			m_info = null;
			m_actionFactory = null;
		}
		
		private function unfreeze():void
		{
			while(numChildren > 0)
			{
				var mc:Bitmap = Bitmap(removeChildAt(0));
				mc.bitmapData.dispose();
			}
			m_unfreeze = true;
			m_actionFactory = new ExplodeParticlesFactory();
			var smokes:Sprite = Sprite(m_actionFactory.makeAction("smoke"));
			smokes.x = 100;
			smokes.y = 100;
			addChild(smokes);
			
			m_actionFactory = new GargoyleFactory();
			addChild(m_actionFactory.makeAction("gargoyle"));
		}
	}
}